package com.my.study.io.output;

import com.my.study.io.Dog;
import lombok.extern.slf4j.Slf4j;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

/**
 * @author Carlos
 * @version 1.0
 * @Description 完成对象序列化
 * @date 2021/6/27 10:23
 **/
@Slf4j(topic = "c.ObjectOutputStreamTest")
public class ObjectOutputStreamTest {

    /**
     * 序列化后，保存的文件格式为.dat
     */
    private static final String PATH = "D:/file/dog.dat";

    public static void main(String[] args) {
        ObjectOutputStream oos = null;

        try {
            oos = new ObjectOutputStream(new FileOutputStream(PATH));

            oos.writeInt(100);
            oos.writeBoolean(true);
            oos.writeChar('a');
            oos.writeDouble(9.5);
            oos.writeUTF("ok!");
            oos.writeObject(new Dog("旺财", 2, "日本", "白色"));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                oos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("序列化完毕~~~");
    }

}
